package org.apache.directory.mavibot.btree;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;

public class MyThreadGetValue extends Thread{
	private long nbInsertCache=0;
	private long begin;
	private long duration;
	private long currenteDate;
	private ArrayList<TimeInsertion> tab=new ArrayList<TimeInsertion>();
	private int numThread;
	private CountDownLatch countDown;
	private PersistedBTree<Long, Long> btree;
	private int cacheSize;
	
	public ArrayList<TimeInsertion> getTab()
	{
		return tab;
	}
	
	public MyThreadGetValue( CountDownLatch c, int n, PersistedBTree<Long, Long> b, long d, int s )
	{
		numThread=n;
		countDown=c;
		btree=b;
		duration=d;
		cacheSize=s;
	}
	
	@Override
	public void run()
	{
		long value;
		long i=0;
		begin=System.currentTimeMillis();
		currenteDate=begin;
		while( currenteDate<begin+duration )
		{
			value=i % cacheSize;
			try {
				btree.get( value );
				nbInsertCache++;
				if( currenteDate+1000<System.currentTimeMillis() )
				{
					tab.add( new TimeInsertion( System.currentTimeMillis()-begin, nbInsertCache ) );
					currenteDate=currenteDate+1000;
				}
			} catch ( IOException e ) {
				e.printStackTrace();
			} catch ( KeyNotFoundException e ) {
				e.printStackTrace();
			}
			i++;
		}
		countDown.countDown();
	}
}
